package cn.hfatec.shms.web.xc.xcJjb.action;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.constants.SysContant;
import cn.hfatec.freemarker.FreeMarkerUtil;
import cn.hfatec.shms.base.ShmsBaseAction;
import cn.hfatec.shms.web.vip.vipXxb.model.VipXxb;
import cn.hfatec.shms.web.xc.xcJjb.model.XcJjb;
import cn.hfatec.shms.web.xc.xcJjb.model.XcJjbParams;
import cn.hfatec.shms.web.xc.xcJjb.service.XcJjbService;
import cn.hfatec.shms.web.xc.xcJjbmx.model.XcJjbmxParams;
import cn.hfatec.shms.web.xc.xcJjbmx.service.XcJjbmxService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;


/**
 * @author hfatec codegen
 * @version 1.0
 *          <p/>
 *          the JAVA code is generate by middlegen
 */
@Controller
@RequestMapping("/xc/xcJjb")
public class XcJjbAction extends ShmsBaseAction {

    //服务层
    @Resource(name = "XcJjbService")
    private XcJjbService xcJjbService;
    //服务层
    @Resource(name = "XcJjbmxService")
    private XcJjbmxService xcJjbmxService;


    /**
     * 查询列表
     */
    @RequestMapping("/list")
    public String list(XcJjbParams xcJjbParams, Page page, Model model) {

        Table table = xcJjbService.queryXcJjbTable(xcJjbParams, page);
        model.addAttribute("xcJjbsTable", table);
        model.addAttribute("xcJjbParams", xcJjbParams);
        return "/xc/xcJjb/list";
    }

    /**
     * 异步查询列表
     */
    @RequestMapping("/ajaxList")
    @ResponseBody
    public String ajaxList(XcJjbParams xcJjbParams, Page page, Model model) {
        try {
            list(xcJjbParams, page, model);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjb/table", model)).toString();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到新增页
     */
    @RequestMapping("/goAdd")
    @ResponseBody
    public String goAdd(Model model) {
        try {
            XcJjb xcJjb = new XcJjb();
            XcJjbmxParams xcJjbmxParams = new XcJjbmxParams();
            List xcJjbmx = xcJjbmxService.getXcJjbmxList();

            model.addAttribute("xcJjb", xcJjb);
            model.addAttribute("jjbmxList", xcJjbmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjb/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goEdit")
    @ResponseBody
    public String goEdit(Integer id, Model model) {

        try {
            XcJjb xcJjb = xcJjbService.getXcJjb(id);
            XcJjbmxParams xcJjbmxParams = new XcJjbmxParams();
            xcJjbmxParams.setJjbId(xcJjb.getJjId());
            List xcJjbmx = xcJjbmxService.getXcJjbmxList(xcJjbmxParams);

            model.addAttribute("xcJjb", xcJjb);
            model.addAttribute("jjbmxList", xcJjbmx);
            return new JsonResponse<Map>(JsonResponse.RETURN_SUCCESS, "", FreeMarkerUtil.render("/xc/xcJjb/form", model)).toString();

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }

    /**
     * 根据IDs删除数据
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String deleteXcJjb(String ids) {
        try {
            List<String> idList = new ArrayList<String>();
            if (ids.length() > 0) {
                idList = Arrays.asList(ids.split(","));
            }
            xcJjbService.deleteXcJjbBatch(idList);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据" + ids + "删除异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.DELETE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse(JsonResponse.RETURN_SUCCESS, SysContant.DELETE_SUCCESS_MESSAGE).toString();
    }

    /**
     * 保存数据
     */
    @RequestMapping("/save")
    @ResponseBody
    public String saveXcJjb(String[] ids, String[] userName, String[] jkxjYj, String[] jkxjJj, String[] tjJj, String[] tjGj, @Valid XcJjb xcJjb, BindingResult result) {

        try {
            if (result.hasErrors()) {
                List<ObjectError> errors = result.getAllErrors();
                return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, errors.get(0).getDefaultMessage()).toString();
            }
            xcJjbService.saveXcJjb(xcJjb, ids, userName, jkxjYj, jkxjJj, tjJj, tjGj);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("数据XcJjb保存异常！");
            return new JsonResponse<String>(JsonResponse.RETURN_FAILURE, SysContant.SAVE_ERROR_MESSAGE).toString();
        }
        return new JsonResponse<String>(JsonResponse.RETURN_SUCCESS, SysContant.SAVE_SUCCESS_MESSAGE).toString();
    }


    /**
     * 导出excel
     */
    @RequestMapping("/export")
    @ResponseBody
    public String export(XcJjbParams xcJjbParams, HttpServletRequest request, HttpServletResponse response) {
        try {
            xcJjbService.exportExcel(xcJjbParams, request, response);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
    /**
     * 跳转到编辑页
     */
    @RequestMapping("/goChoose")
    @ResponseBody
    public String goChoose(String entryDate) {
        try {
            XcJjb xcJjb = xcJjbService.getJjbBack(entryDate);
            if (xcJjb != null) {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", xcJjb).toString();
            } else {
                return new JsonResponse(JsonResponse.RETURN_SUCCESS, "", new XcJjb()).toString();
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonResponse.error;
    }
}
